import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_audioplayers.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_title_content_view.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/modules/application/intelligent_inspection_page/inspection_list_page/inspection_details_page/inspection_details_controller.dart';
import 'package:url_launcher/url_launcher.dart';

/// 台账详情
class InspectionDetailsPage extends StatelessWidget {
  InspectionDetailsPage({super.key});

  final controller = Get.put(InspectionDetailsController());

  /// 获取详情视图
  Widget _getDetailsView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        AppGaps.vGap6,
        Row(
          children: [
            Text(
              '巡检描述:',
              style: TextStyle(
                fontSize: 13.sp,
                height: 1.2,
                color: const Color(0xffA5A6B5),
                overflow: TextOverflow.ellipsis,
                fontWeight: FontWeight.w400,
              ),
            ),
            Text(
              controller.detailEntity.inspectionDescription ?? '',
              style: TextStyle(
                fontSize: 13.sp,
                height: 1.2,
                color: const Color(0xff495061),
                overflow: TextOverflow.ellipsis,
                fontWeight: FontWeight.w400,
              ),
            ),
          ],
        ),
        AppGaps.vGap12,
        Text(
          '拍照:',
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xffA5A6B5),
            overflow: TextOverflow.ellipsis,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.vGap6,
        Wrap(
            spacing: 5.w,
            children: controller.detailEntity.inspectionPhotos!.map<Widget>((item) {
              return Container(
                width: 74.w,
                height: 74.w,
                margin: EdgeInsets.only(bottom: 10.w),
                decoration: BoxDecoration(
                  color: const Color(0xffF7F7FB),
                  border: Border.all(
                    color: const Color(0xffEBEDF0),
                  ),
                  borderRadius: BorderRadius.circular(8.w),
                  image: DecorationImage(
                    image: NetworkImage(Interface.fileUrl + item),
                    fit: BoxFit.cover,
                  ),
                ),
              ).clickShowLarge(url: Interface.fileUrl + item);
            }).toList()),
        AppGaps.vGap12,
        Text(
          '视频:',
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xffA5A6B5),
            overflow: TextOverflow.ellipsis,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.vGap6,
        Wrap(
            spacing: 5.w,
            children: controller.detailEntity.inspectionVideos!.map<Widget>((item) {
              return Container(
                width: 160.w,
                height: 90.w,
                margin: const EdgeInsets.only(bottom: 10),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: const Color(0xff060B15),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: IconButton(
                  icon: const Icon(
                    Icons.play_circle,
                    color: Colors.white,
                    size: 30,
                  ),
                  onPressed: () {
                    FileUtils.openFile(Interface.fileUrl + item);
                  },
                ),
              );
            }).toList()),
        AppGaps.vGap12,
        Text(
          '语音:',
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xffA5A6B5),
            overflow: TextOverflow.ellipsis,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.vGap6,
        controller.detailEntity.patrolVoices!.isNotEmpty
            ? CustomAudioplayers(
                audioUrl: controller.detailEntity.patrolVoices!,
                isEnabled: false,
              )
            : Container(),
      ],
    );
  }

  Widget _widget(int i) {
    return Container(
      width: 40.w,
      height: 18.w,
      decoration: BoxDecoration(
        borderRadius: const BorderRadius.only(
          topRight: Radius.circular(8),
          topLeft: Radius.circular(8),
          bottomRight: Radius.circular(8),
        ),
        color: _typeColor(i),
      ),
      child: Center(
        child: Text(
          _type(i),
          style: TextStyle(color: Colors.white, fontSize: 10.sp),
        ),
      ),
    );
  }

  /// 类型判断
  String _type(int i) {
    switch (i) {
      case 0:
        return '正常';
      case 2:
        return '漏检';
      case 3:
        return '补检';
      default:
        return '无';
    }
  }

  Color _typeColor(int i) {
    switch (i) {
      case 0:
        return const Color(0xFF00CE78);
      case 2:
        return const Color(0xFFFD6B6B);
      case 3:
        return const Color(0xFFFC9900);
      default:
        return Colors.grey;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              title: Text(
                '计划台账',
                style: TextStyle(
                  fontSize: 18.sp,
                  color: Colors.white,
                  fontWeight: FontWeight.w400,
                ),
              ),
              leading: const BackButton(
                color: Colors.white,
              ),
              iconButtonColor: Colors.white,
              backgroundColor: const Color(0xFF5C6DFF),
            ),
            body: Container(
                color: const Color(0xFFF5F5F6),
                child: Stack(
                  children: [
                    Container(
                      height: 84.w,
                      width: AppDimens.sh,
                      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.w),
                      color: const Color(0xFF5C6DFF),
                    ),
                    Container(
                      alignment: Alignment.topCenter,
                      child: Column(
                        children: [
                          // 基础信息
                          Container(
                            width: AppDimens.sh,
                            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 10.w),
                            color: const Color(0xFF5C6DFF),
                            child: Row(
                              children: [
                                SvgPicture.asset(
                                  AppImages.icon_title_task,
                                  width: 17.w,
                                  height: 17.w,
                                ),
                                AppGaps.hGap6,
                                Expanded(
                                    child: Text(
                                  controller.detailEntity.riskUnitName ?? '',
                                  style: TextStyle(
                                    fontSize: 18.sp,
                                    color: Colors.white,
                                    overflow: TextOverflow.ellipsis,
                                    fontWeight: FontWeight.w500,
                                  ),
                                )),
                              ],
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 16.w),
                            child: CustomTitleContentView(
                              title: Row(
                                children: [
                                  SvgPicture.asset(
                                    AppImages.icon_title,
                                    width: 20.w,
                                    height: 20.w,
                                  ),
                                  Expanded(
                                    child: Container(
                                      margin: EdgeInsets.only(left: 6.w),
                                      child: Text(
                                        controller.detailEntity.troubleshootContent ?? '',
                                        overflow: TextOverflow.ellipsis,
                                        style: TextStyle(
                                          color: Colors.black,
                                          fontSize: 15.sp,
                                          fontWeight: FontWeight.w500,
                                        ),
                                      ),
                                    ),
                                  ),
                                  _widget(controller.detailEntity.checkStatus ?? 0)
                                ],
                              ),
                              content: SingleChildScrollView(
                                child: _getDetailsView(),
                              ),
                            ),
                          ),
                        ],
                      ),
                    )
                  ],
                )),
          );
        });
  }
}
